package com.gwtt.simulator.netconf.message;

import com.gwtt.simulator.netconf.subsystem.NetconfClient;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;

import java.util.Date;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

@Slf4j
public class NotificationSender {

    private static Set<NetconfClient> clientSet = new ConcurrentSkipListSet<>();

    public static void start() {
		/*try {
			SchedulerFactory schedulerFactory = new StdSchedulerFactory();
			Scheduler scheduler = schedulerFactory.getScheduler();
			
			addJob(scheduler, "0/10 * * * * ?", "demo");
			
			scheduler.start();
		} catch (SchedulerException e) {
			log.error("Quartz start err");
		}
*/
        try {
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static void subscription(NetconfClient client) {
        clientSet.add(client);
    }

    public static void unsubscription(NetconfClient client) {
        clientSet.remove(client);
    }

    public static Set<NetconfClient> getClientSet() {
        return clientSet;
    }

    private static void addJob(Scheduler scheduler, String cronExpression, String jobKey) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(NotificationJob.class).withIdentity(jobKey,
                "notification-group").build();

        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobKey).startAt(new Date())
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();

        scheduler.scheduleJob(jobDetail, trigger);
    }

}
